﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Net;
using Newtonsoft.Json;
using System.Net.Sockets;

namespace Emoney.UserTraining.Comm
{
   public class Pub
    {
       public static string LoadData(string url, string ecode)
       {
           string rel = "";
           try
           {
               HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create(url);
               hwr.Method = "GET";
               hwr.KeepAlive = true;
               hwr.Accept = "*/*";
               hwr.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
               hwr.Headers.Add("Accept-Language", "zh-cn");
               hwr.Headers.Add("UA-CPU", "x86");
               hwr.Headers.Add("Cookie", "JSESSIONID=1DCDFF4036AD4EFE47A212E22FD8EFC1");
               //hwr.Referer = "";
               hwr.ServicePoint.Expect100Continue = false;
               HttpWebResponse response = (HttpWebResponse)hwr.GetResponse();
               System.IO.Stream myResponseStreame = response.GetResponseStream();
               System.IO.StreamReader sdr = new System.IO.StreamReader(myResponseStreame, Encoding.GetEncoding(ecode));
               rel = sdr.ReadToEnd().ToString();
           }
           catch (Exception ex) { }
           return rel;
       }
        public static string LoadData(string url)
        {
            string rel = "";
            try
            {
                //StringBuilder sb = new StringBuilder();
                //WebClient wc = new WebClient();
                //wc.Encoding = System.Text.Encoding.Default;
                //Client client = new Client();
                //rel = client.GetClientBySocket(url, System.Text.Encoding.Default);

               // url = HttpUtility.UrlDecode(url);
                HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create(url);

                hwr.Method = "GET";
                hwr.KeepAlive = true;
                hwr.Accept = "*/*";
                hwr.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
                hwr.Headers.Add("Accept-Language", "zh-cn");
                hwr.Headers.Add("UA-CPU", "x86");
                hwr.Headers.Add("Cookie", "JSESSIONID=1DCDFF4036AD4EFE47A212E22FD8EFC1");
                //hwr.Referer = "";
                hwr.ServicePoint.Expect100Continue = false;
                HttpWebResponse response = (HttpWebResponse)hwr.GetResponse();
                System.IO.Stream myResponseStreame = response.GetResponseStream();
                System.IO.StreamReader sdr = new System.IO.StreamReader(myResponseStreame, Encoding.GetEncoding("gb2312"));
                rel = sdr.ReadToEnd().ToString();
            }
            catch (Exception ex) { }
            return rel;
        }
        public static string GetClientBySocket(string UrlString)
        {
            Encoding encode = Encoding.GetEncoding("gb2312");
            string host;
            int port = 80;
            if (UrlString.StartsWith("http://"))
            {
                string temp = UrlString.Trim();
                temp = temp.Substring(7, temp.Length - 7);
                if (temp.IndexOf("/") == -1)
                {
                    host = temp;
                }
                else
                {
                    host = temp.Substring(0, temp.IndexOf("/"));
                }
                int portIndex = host.IndexOf(":");
                if (portIndex != -1)
                {
                    port = int.Parse(host.Substring(portIndex + 1));
                    host = host.Substring(0, portIndex);
                }
            }
            else
            {
                host = UrlString.Substring(0, UrlString.IndexOf("/"));
            }

            IPAddress[] ips = Dns.GetHostAddresses(host);
            IPAddress ip = ips[0];
            IPEndPoint serverhost = new IPEndPoint(ip, port);
            Socket clientSocket = null;
            try
            {
                clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                clientSocket.Connect(serverhost);

                string httpReq = "GET " + UrlString + " HTTP/1.1\r\n";
                httpReq += "Host:" + host + "\r\n";
                httpReq += "Connection: close\r\n\r\n";
                StringBuilder txtHTML = new StringBuilder();

                clientSocket.Send(encode.GetBytes(httpReq));
                Byte[] buffer = new byte[10240];
                int byteCount = clientSocket.Receive(buffer, buffer.Length, 0);
                List<byte> bLst = new List<byte>();
                AddByte(bLst, buffer, byteCount);
                while (byteCount > 0)
                {
                    byteCount = clientSocket.Receive(buffer, buffer.Length, 0);
                    AddByte(bLst, buffer, byteCount);

                }
                byte[] bbs = new byte[bLst.Count];
                for (int i = 0; i < bLst.Count; i++)
                {
                    bbs[i] = bLst[i];
                }

                var str = encode.GetString(bbs, 0, bbs.Length);
                //判断页面状态
                string strState = str.Substring(0, str.IndexOf("\r\n"));
                var ss = strState.Substring(strState.IndexOf(' ') + 1);
                if (ss != "200 OK")
                {
                    throw new Exception(ss);
                }

                str = str.Substring(str.IndexOf("\r\n\r\n") + 4);
                return str;
            }
            catch (Exception err)
            {
                throw err;
            }
            finally
            {
                if (clientSocket != null)
                {
                    try
                    {
                        clientSocket.Close();
                    }
                    catch
                    {
                    }
                }
            }
        }

        static void AddByte(List<byte> lst, byte[] data, int count)
        {
            for (int i = 0; i < count; i++)
            {
                lst.Add(data[i]);
            }
        }


        public static string ShowAd(string tid, string txt, string txt2)
        {
            string rel = "";
            switch (tid)
            {
                case "14":
                    rel = txt;
                    break;
                case "16":
                    rel = txt2;
                    break;
            }
            return rel;
        }

        /// <summary>
        /// pid升级9位后转3为，做软件3位9位支持
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        public static string GetPid_9To3(string pid)
        {
            string newpid = pid;
            if (pid.Length == 9)
            {
                switch (pid)
                {
                    case "201000000"://决策版
                        newpid = "202";
                        break;
                    case "202000000"://新决策版
                        newpid = "202";
                        break;
                    case "202020000"://新决策版（双平台）
                        newpid = "202";
                        break;
                    case "202100000"://决策加强版
                        newpid = "202100000";
                        break;
                    case "202000001"://决策版6.0(电商版)
                        newpid = "202";
                        break;
                    case "109000000"://盯盘先锋
                        newpid = "109";
                        break;
                    case "131000000"://主力版
                        newpid = "131";
                        break;
                    case "100000000"://乾坤版
                        newpid = "100";
                        break;
                    case "100000001"://乾坤版(电商版)
                        newpid = "100";
                        break;
                    case "100002000"://乾坤体验版
                        newpid = "100";
                        break;
                    case "152000000"://领先版
                        newpid = "152";
                        break;
                    case "121000000"://睿讯通
                        newpid = "121";
                        break;
                    case "121002000"://睿讯通体验
                        newpid = "121";
                        break;
                    case "230000000"://经典版(L1)
                        newpid = "230";
                        break;
                    case "151000000"://经典版(L2)
                        newpid = "151";
                        break;
                    case "141100000"://新股模型(抢新版)
                        newpid = "1411";
                        break;
                    case "141200000"://涨停模型
                        newpid = "1412";
                        break;
                    case "113100000"://稳赢1号
                        newpid = "1131";
                        break;
                    case "113200000"://稳赢2号
                        newpid = "1132";
                        break;
                    case "113300000"://稳赢蓝筹
                        newpid = "1133";
                        break;
                    case "113400000"://稳赢战略
                        newpid = "1134";
                        break;
                    case "114100000"://智赢投资组合
                        newpid = "1141";
                        break;
                    case "157000000"://多空复利(资产倍增)
                        newpid = "157";
                        break;
                    case "158000000"://金管家
                        newpid = "158";
                        break;
                    case "211000000"://低额产品（小额版）
                        newpid = "211";
                        break;
                    case "800000000"://手机PAD版
                        newpid = "800";
                        break;
                    case "700000000"://手机决策版
                        newpid = "700";
                        break;
                    case "710000000"://手机主力版
                        newpid = "710";
                        break;
                    case "220000000"://股票医生
                        newpid = "220";
                        break;
                    case "210000000"://腾讯操盘手pc版本
                        newpid = "212";
                        break;
                    case "210010000"://腾讯操盘手手机版
                        newpid = "212";
                        break;
                }
            }
            return newpid;
        }
    }
}
